namespace Angelus.Oracle.Entity.Common.Entity
{
    using System.Data;
    using System.Data.Common;

    /// <summary>
    /// Description of OracleServerVersion.
    /// </summary>
    internal class StoreVersion
    {
        #region Fields

        private readonly DbConnection _connection;
        private string _version;

        #endregion

        #region Constructors

        /// <summary>
        /// 
        /// </summary>
        /// <param name="connection"></param>
        internal StoreVersion(DbConnection connection)
        {
            this._connection = connection;
        }

        #endregion

        #region Properties

        /// <summary>
        /// 
        /// </summary>
        internal string Version
        {
            get
            {
                if (null == this._version)
                {
                    this._version = this.GetServerVersion();
                }

                return this._version;
            }
        }

        #endregion

        #region Private Implementation

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string GetServerVersion()
        {
            bool opened = this._connection.State == ConnectionState.Open;

            try
            {
                if (!opened)
                {
                    this._connection.Open();
                }

                return this._connection.ServerVersion;
            }
            finally
            {
                if (!opened)
                {
                    this._connection.Close();
                }
            }
        }

        #endregion
    }
}